package cn.stronglink.asset.module.assetmanage.pulloff.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetRecord;
import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.PullOff;
import cn.stronglink.asset.module.assetRecord.mapper.AssetRecordMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.pulloff.mapper.PullOffMapper;
import cn.stronglink.asset.module.assetmanage.pulloff.vo.PullOffDetailVo;
import cn.stronglink.asset.module.rfidState.mapper.RfidStateMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class PullOffService {

	@Autowired
	private PullOffMapper pullOffMapper;
	@Autowired
	private ItassetMapper itassetMapper;
	@Autowired
	private AssetRecordMapper assetRecordMapper;
	@Autowired
	private RfidStateMapper rfidStateMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<PullOff> queryPullOffList(Page<PullOff> page, Map<String, Object> params) {
		
		// 首先查询出所有下架单
		List<PullOff> PullOffList =pullOffMapper.queryPullOffList(page,params);
		
		//再根据每个下架单号查询出所有明细，并且把每个明细的设备型号，所在机柜拼接存放到下架单中。
		for(int i=0;i<PullOffList.size();i++){
			PullOff PullOff=PullOffList.get(i);
			Long pullOffId=PullOff.getId();
			String modelNames="";
			String rackNames="";
			List<PullOffDetailVo> PullOffDetailList =pullOffMapper.queryPullOffDetailByParentId(pullOffId);
			if(PullOffDetailList!=null&&PullOffDetailList.size()>0){
				if(PullOffDetailList.size()>1){
					for(PullOffDetailVo PullOffDetail:PullOffDetailList){
						modelNames+=PullOffDetail.getModelName()+"/";
						rackNames+=PullOffDetail.getRackName()+"/";
					}
				}else{
					for(PullOffDetailVo PullOffDetail:PullOffDetailList){
						modelNames+=PullOffDetail.getModelName();
						rackNames+=PullOffDetail.getRackName();
					}
				}
				
			}
			
			PullOff.setModelNames(modelNames);
			PullOff.setRackNames(rackNames);
		}
		page.setRecords(PullOffList);
		return page;	
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public PullOff queryPullOffById(Long id) {
		// 首先查询出下架单 然后查询改下架单下所有的下架单明细
		PullOff pullOff =pullOffMapper.queryPullOffById(id);
		List<PullOffDetailVo> detail = pullOffMapper.qryPullOffDetailVo(id);
		pullOff.setDetailList(detail);
		return	pullOff;
	}

	public void remove(Long id) {
		try {
			pullOffMapper.removeDetail(id);
			pullOffMapper.remove(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void edit(PullOff PullOff) {
		try {
			pullOffMapper.updatePullOff(PullOff);
			for(PullOffDetailVo detail :PullOff.getDetailList()){
				if (detail.getId() == null) {
					detail.setId(IdWorker.getId());
					detail.setStatus(4);
					detail.setAssetId(Long.parseLong(detail.getAssetId_()));
					detail.setCreateBy(WebUtil.getCurrentUser());
					detail.setCreateTime(new Date());
					pullOffMapper.insertPullOffDetail(detail);		
				}else{
					detail.setUpdateTime(new Date());
					detail.setUpdateBy(WebUtil.getCurrentUser());
					pullOffMapper.updatePullOffDetail(detail);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public int create(PullOff pullOff) {
		pullOff.setType(1);
		pullOff.setStatus(1);
		pullOff.setCreateBy(WebUtil.getCurrentUser());
		pullOff.setCreateTime(new Date());
		int count =pullOffMapper.insertPullOff(pullOff);	
		if(pullOff.getDetailList()!=null&&pullOff.getDetailList().size()>0){
			for(PullOffDetailVo detail:pullOff.getDetailList()){
				detail.setId(IdWorker.getId());
				detail.setStatus(2);
				detail.setPullOffId(pullOff.getId());
				detail.setAssetId(Long.parseLong(detail.getAssetId_()));
				detail.setCreateBy(WebUtil.getCurrentUser());
				detail.setCreateTime(new Date());
				pullOffMapper.insertPullOffDetail(detail);
			}
		}
		return count;
	}

	public void doPullOff(PullOff pullOff) {
		pullOff.setStatus(3);
		pullOff.setUpdateBy(WebUtil.getCurrentUser());
		pullOff.setUpdateTime(new Date());
		pullOffMapper.updatePullOff(pullOff);	
		if(pullOff.getDetailList()!=null&&pullOff.getDetailList().size()>0){
			for(PullOffDetailVo detail:pullOff.getDetailList()){
				detail.setId(Long.parseLong(detail.getDetailId()));
				detail.setStatus(1);
				detail.setAssetId(Long.parseLong(detail.getAssetId_()));
				detail.setPullOffId(pullOff.getId());
				detail.setUpdateBy(WebUtil.getCurrentUser());
				detail.setUpdateTime(new Date());
				pullOffMapper.updatePullOffDetail(detail);
				
				//更新资产表中状态为已下架
				//更新资产表状态为已上架
				ItAsset asset = new ItAsset();
				asset.setId(Long.parseLong(detail.getAssetId_()));
				asset.setStatus(1);
				itassetMapper.updateItAsset(asset);
				
				rfidStateMapper.deleteRfidState(detail.getRfidNo());
				
				//插入资产生命周期表记录
				AssetRecord re = new AssetRecord();
				re.setId(IdWorker.getId());
				re.setAssetId(Long.parseLong(detail.getAssetId_()));
				re.setType(3);
				re.setCreateBy(WebUtil.getCurrentUser());
				re.setCreateTime(new Date());
				assetRecordMapper.create(re);
				 
				
			}
		}
		
	}

}
